// ActionScript file

import Modules.NavigationInfo;
import Modules.PostLogin;

import com.desidea.common.CommonClass;
import com.desidea.common.MenuConstants;
import com.desidea.components.grid.SuperGrid;
import com.desidea.components.grid.SuperGridEvent;
import com.desidea.utils.DirtyChecker;
import com.desidea.utils.IDirtyChecker;
import com.desidea.webservice.PartnerService;
import com.desidea.webservice.ProcessServiceRequestResultEvent;

import mx.controls.Alert;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.Application;
import mx.rpc.events.FaultEvent;

private function init():void{
}
private function windowCreated():void{
	myActiveAccountsGrid.addEventListener(SuperGridEvent.CLICKED, accountsGridButtonColumnClicked);
	myActivitiesGrid.addEventListener(SuperGridEvent.CLICKED, activitiesGridButtonColumnClicked);
	myTasksGrid.addEventListener(SuperGridEvent.CLICKED, tasksGridButtonColumnClicked);
	myOpportunitiesGrid.addEventListener(SuperGridEvent.CLICKED, oppsGridButtonColumnClicked);
	loadData();
}

private function loadData():void{
	loadActiveAccounts();
	loadActivities();
	loadTasks();
	loadOpportunities();

}

//Load Active accounts
private function loadActiveAccounts():void{
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}else{
		DirtyChecker.getInstance().resetDirtyChecker();
	}
	
	
	var params:String = "OPERATION=DASHBOARD_ACTIVEACCOUNTS" +
//						"&ISACTIVE=1";
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForActiveAccountsList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.ModuleCvs);
}

private function callForActiveAccountsList(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();	
	var response:XML = XML(event.result);
//	Alert.show(response.toString());
	if(response.RESPONSE.RESULT == "E"){
		Alert.show("Unable to load Active Accounts");
	}else if(response.RESPONSE.RESULT == 'S'){
		this.myActiveAccountsGrid.dataProvider = response;
		this.myActiveAccountsGrid.refreshDataProvider(0);
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
	setAAGridColumnWidths(myActiveAccountsGrid);
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
//	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}

private function setAAGridColumnWidths(grid:SuperGrid):void{
	setColumnWidth(grid, 1, 120);
	setColumnWidth(grid, 2, 80);
	setColumnWidth(grid, 3, 50);
	setColumnWidth(grid, 4, 65);
}
//------------
//Load Activities
private function loadActivities():void{
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}else{
		DirtyChecker.getInstance().resetDirtyChecker();
	}
	
	
	var params:String = "OPERATION=DASHBOARD_ACTIVITIESTHISWEEK" +
//						"&ISACTIVE=1";
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForActivitiesList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.ModuleCvs);
}

private function callForActivitiesList(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();	
	var response:XML = XML(event.result);
//	Alert.show(response.toString());
	if(response.RESPONSE.RESULT == "E"){
		Alert.show("Unable to load Activities This Week");
	}else if(response.RESPONSE.RESULT == 'S'){
		this.myActivitiesGrid.dataProvider = response;
		this.myActivitiesGrid.refreshDataProvider(0);
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
	setActivitiesGridColumnWidths(myActivitiesGrid);
}

private function setActivitiesGridColumnWidths(grid:SuperGrid):void{
	setColumnWidth(grid, 1, 120);
	setColumnWidth(grid, 2, 100);
	setColumnWidth(grid, 3, 90);
	setColumnWidth(grid, 4, 80);
}
//----------------
private function loadTasks():void{
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}else{
		DirtyChecker.getInstance().resetDirtyChecker();
	}
	
	
	var params:String = "OPERATION=DASHBOARD_TASKSTHISWEEK" +
//						"&ISACTIVE=1";
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForTasksList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.ModuleCvs);
}

private function callForTasksList(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();	
	var response:XML = XML(event.result);
//	Alert.show(response.toString());
	if(response.RESPONSE.RESULT == "E"){
		Alert.show("Unable to load Tasks This Week");
	}else if(response.RESPONSE.RESULT == 'S'){
		this.myTasksGrid.dataProvider = response;
		this.myTasksGrid.refreshDataProvider(0);
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
	setTasksGridColumnWidths(myTasksGrid);
}

private function setTasksGridColumnWidths(grid:SuperGrid):void{
	setColumnWidth(grid, 1, 120);
	setColumnWidth(grid, 2, 100);
	setColumnWidth(grid, 3, 90);
	setColumnWidth(grid, 4, 80);
}
//----------------
private function loadOpportunities():void{
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}else{
		DirtyChecker.getInstance().resetDirtyChecker();
	}
	
	
	var params:String = "OPERATION=DASHBOARD_OPPORTUNITIESINPROGRESS" +
//						"&ISACTIVE=1";
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForOpportunitiesList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.ModuleCvs);
}

private function callForOpportunitiesList(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();	
	var response:XML = XML(event.result);
//	Alert.show(response.toString());
	if(response.RESPONSE.RESULT == "E"){
		Alert.show("Unable to load Opportunities In Progress");
	}else if(response.RESPONSE.RESULT == 'S'){
		this.myOpportunitiesGrid.dataProvider = response;
		this.myOpportunitiesGrid.refreshDataProvider(0);
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
	setOpportunitiesGridColumnWidths(myOpportunitiesGrid);
}

private function setOpportunitiesGridColumnWidths(grid:SuperGrid):void{
	setColumnWidth(grid, 1, 130);
	setColumnWidth(grid, 2, 80);
	setColumnWidth(grid, 3, 80);
}

private function accountsGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "accounts");
}

private function activitiesGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "activities");
}

private function tasksGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "tasks");
}

private function oppsGridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event, "opps");
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	switch(moduleCode){
		case "accounts":
			var accNI:NavigationInfo = new NavigationInfo("",MenuConstants.CMD, MenuConstants.MY_ACCOUNT, event.data.data["key"].toString());
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(accNI);
			break;
		case "activities":
			var actNI:NavigationInfo = new NavigationInfo("",MenuConstants.CMD, MenuConstants.MY_ACTIVITIES, event.data.data["key"].toString());
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(actNI);
			break;
		case "tasks":
			var taskNI:NavigationInfo = new NavigationInfo("",MenuConstants.CMD, MenuConstants.MY_TASKS, event.data.data["key"].toString());
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(taskNI);
			break;
		case "opps":
			var oppsNI:NavigationInfo = new NavigationInfo("",MenuConstants.CMD, MenuConstants.MY_OPPORTUNITIES, event.data.data["key"].toString());
			(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(oppsNI);
			break;
	}
}

private function setColumnWidth(grid:SuperGrid, colIndex:int, colWidth:int, alignment:String = "left"):void{
	if(grid.GridComponent.columnCount > colIndex)	
		grid.GridComponent.columns[colIndex].width = colWidth;
		
		var col:DataGridColumn = grid.GridComponent.columns[colIndex];
		col.setStyle("textAlign", alignment);
}
